<?php

class calendarEspecial extends Object implements especialInterface{
    
    public function validate($campo, &$valor){
        if(trim($valor) == "") return true;
        $temp = explode(" ", $valor);
        foreach($temp as &$v)$v = trim($v);
        $val   = implode(" ", $temp);
        $valor = trim(str_replace('0000-00-00', '', $val));
        if(!$this->isValidDate($valor) && !$this->isValidDateTime($valor))return false;
        if(!$this->validateMinDate($campo, $valor)) return false;
        //if(!$this->validateMaxDate($campo, $valor)) return false;
        return true;
    }
    
    public function js($campo, $array, $form){
        
        $date = $form->getVar($campo);
        if($date != ""){
            if($date != "0000-00-00" && $date != "0000-00-00 00:00:00"){
                $date = explode(" ", $date);
                $data = array_shift($date);
                $hora = array_shift($date);
                $data = convertData($data);
                $data = "$data $hora"; 
            }else $data = "";
            $form->setVars($campo, $data, true);
        }elseif(isset($_POST[$campo])) $date = $_POST[$campo];
        
        $array['type'] = isset($array['type'])?$array['type']:"date";
        //$this->LoadJsPlugin("formulario/calendar", 'calendar');
        $this->LoadJsPlugin("formulario/mobiscroll", 'calendar');
        $mindate = isset( $array['mindate'])? $array['mindate']:"";
        $maxdate = isset( $array['maxdate'])? $array['maxdate']:"";
        
        $type = "";
        $this->calendar->setEvents($campo, $mindate, $maxdate);
        switch ($array['type']){
            case 'timestamp':
            case 'datetime':
                $this->calendar->datetime($campo);
                break;
            case 'time':
                if(!MOBILE) $this->calendar->time($campo);
                else $type = "time";
                break;
            default :
                if(!MOBILE) $this->calendar->draw($campo);
                else $type = "date";
        }
        $this->calendar->clearEvents();
        $form->text($campo, $array['name'], '', @$array['description'], "", $type);
    }
    
    public function isValidDate($dateTime){
       if($dateTime == "") return true;
       if (preg_match("/^(\d{4})-(\d{2})-(\d{2})$/", $dateTime, $matches)) {
          if (checkdate($matches[2], $matches[3], $matches[1])) return true;
       }
       $this->setErrorMessage("Data no formato inválido, Digite (DD/MM/AAAA)");
       return false;
    }
    
    // valida data e hora no formato 2009-01-14 09:00:00
    public function isValidDateTime($dateTime){
       if($dateTime == "") return true;
       if (preg_match("/^(\d{4})-(\d{2})-(\d{2}) ([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/", $dateTime, $matches)) {
          if (checkdate($matches[2], $matches[3], $matches[1]))return true;
       }
       $this->setErrorMessage("Data no formato inválido, Digite (DD/MM/AAAA HH:MM:SS)");
       return false;
    }
    
    
    private function validateMinDate($campo, $valor){
        $dados = armazem::getInstanceOf()->getVar($campo);
        $mindate = isset($dados['mindate'])? $dados['mindate']:"";
        if($mindate == "") return true;
        $campsrc = $dados['name'];
        if($mindate == "now"){
            if(timeResource::diffDate($valor, '', 'Mi') > 0){
                $this->setErrorMessage("O valor do campo $campsrc deve ser maior do que a data/hora atual");
                return false;
            }
            return true;
        }

        if($this->isValidDate($mindate) || $this->isValidDateTime($mindate)){
            if(timeResource::diffDate($valor, $mindate) > 0){
                $this->setErrorMessage("O valor do campo $campsrc deve ser maior ou igual a $mindate");
                return false;
            }
            return true;
        }

        $dados2  = armazem::getInstanceOf()->getVar($mindate);
        $campdst = $dados2['name'];
        $post = armazem::getInstanceOf()->getVar('post');
        if(!isset($post[$mindate])) return true;
        
        $datecamp = timeResource::getDbDate($post[$mindate]);
        if(timeResource::diffDate($valor, $datecamp) > 0){
            $this->setErrorMessage("O valor do campo $campsrc deve ser maior ou igual ao valor do campo $campdst");
            return false;
        }
        return true;
    }
    
}

?>
